<template>
	<view class="container">
		<u--textarea v-model="content" height="180" placeholder="有问必答，百万同行为你解答汽车相关问题！" border="none"></u--textarea>
		<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="fileList" multiple
			:maxCount="maxCount" :width="240" :height="240" :accept="accept"></u-upload>
		<view class="footer">
			<u-button :disabled="disabled" type="primary" color="#06A972" text="发布" @click="shareUploadVideo"></u-button>
		</view>
	</view>
</template>


<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	export default {
		data() {
			return {
				content: '',
				maxCount: 1,
				fileList: [],
				list: [],
				fileListLen: 0,
				accept: 'video',
				carId: 0,
				disabled: false
			}

		},

		onLoad(option) {
			let that = this,
				title, year;
			that.carId = option.carId;
			title = option.title;
			year = option.year;
			console.log(option);
			if (!that.carId) {
				return that.$util.Tips({
					title: '车型不存在'
				}, {
					tab: 3
				});
			}
			uni.setNavigationBarTitle({
				title: title + ' ' + year
			})
		},

		methods: {

			// 删除图片
			deletePic(event) {
				this[`${event.name}`].splice(event.index, 1)
			},
			// // 新增图片
			afterRead(event) {
				let that = this;
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				that.list = [].concat(event.file)
				that.fileListLen = that[`${event.name}`].length
				that.list.map((item) => {
					that[`${event.name}`].push({
						...item,
						status: 'success',
						message: ''
					})
				})
				console.log(that.fileList);
			},

			async shareUploadVideo() {
				let that = this;
				
				that.disabled = true;
				if (!that.content) return that.$util.Tips({
					title: '请输入内容'
				})
				
				uni.showLoading({
					mask: true
				});
				for (let i = 0; i < that.list.length; i++) {
					const result = await that.uploadFilePromise(that.list[i].url);
					uni.hideLoading();
					if (result.resultCode == 1) {
						that.$util.Tips({
							title: '发布成功',
						}, {
							tab: 3
						})
					} else {
						that.$util.Tips({
							title: '发布失败',
						});
					}
				}
			},

			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/car/shareUploadVideo',
						filePath: url,
						name: 'shareVideo',
						formData: {
							carId: this.carId,
							userInfoId: this.$store.state.app.uid,
							shareContent: this.content,
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data))
							}, 1000)
						}
					});
				})
			},


		}
	}
</script>

<style>
	.container {
		padding: 40rpx;
	}

	.u-upload__button .u-icon__icon {
		font-size: 28px !important;
	}

	.u-upload__deletable__icon .u-icon__icon {
		font-size: 20rpx !important;
		line-height: 20rpx !important;
	}

	.u-upload__success__icon .u-icon__icon {
		font-size: 24rpx !important;
		line-height: 24rpx !important;
	}

	.u-upload__wrap__preview__other {
		width: 240rpx !important;
		height: 240rpx !important;
	}

	.u-upload__wrap__preview__other .u-icon__icon {
		font-size: 36px !important;
		line-height: 36px !important;
	}
</style>